home *** CD-ROM | disk | FTP | other *** search
/ Java 1996 August / Java - Summer 1996.iso / rockridge / java / threads / betaclasses / SortItem.java < prev    next >
Encoding:
Java Source  |  1995-11-13  |  5.2 KB  |  234 lines

  1. /*
  2.  * @(#)SortItem.java    1.17f 95/04/10 James Gosling
  3.  *
  4.  * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19.  
  20. import java.awt.*;
  21. import java.io.InputStream;
  22. import java.util.Hashtable;
  23. import java.net.*;
  24.  
  25. /**
  26.  * A simple applet class to demonstrate a sort algorithm.
  27.  * You can specify a sorting algorithm using the "alg"
  28.  * attribyte. When you click on the applet, a thread is
  29.  * forked which animates the sorting algorithm.
  30.  *
  31.  * @author James Gosling
  32.  * @version     1.17f, 10 Apr 1995
  33.  */
  34. public class SortItem extends java.applet.Applet implements Runnable {
  35.     /**
  36.      * The thread that is sorting (or null).
  37.      */
  38.     private Thread kicker;
  39.  
  40.     /**
  41.      * The array that is being sorted.
  42.      */
  43.     int arr[];
  44.  
  45.     /**
  46.      * The high water mark.
  47.      */
  48.     int h1 = -1;
  49.  
  50.     /**
  51.      * The low water mark.
  52.      */
  53.     int h2 = -1;
  54.  
  55.     /**
  56.      * The name of the algorithm.
  57.      */
  58.     String algName;
  59.  
  60.     /**
  61.      * The sorting algorithm (or null).
  62.      */
  63.     SortAlgorithm algorithm;
  64.  
  65.     /**
  66.      * Fill the array with random numbers from 0..n-1.
  67.      */
  68.     void scramble() {
  69.     int a[] = new int[size().height / 2];
  70.     double f = size().width / (double) a.length;
  71.     for (int i = a.length; --i >= 0;) {
  72.         a[i] = (int)(i * f);
  73.     }
  74.     for (int i = a.length; --i >= 0;) {
  75.         int j = (int)(i * Math.random());
  76.         int t = a[i];
  77.         a[i] = a[j];
  78.         a[j] = t;
  79.     }
  80.     arr = a;
  81.     }
  82.  
  83.     /**
  84.      * Pause a while.
  85.      * @see SortAlgorithm
  86.      */
  87.     void pause() {
  88.     pause(-1, -1);
  89.     }
  90.  
  91.     /**
  92.      * Pause a while, and draw the high water mark.
  93.      * @see SortAlgorithm
  94.      */
  95.     void pause(int H1) {
  96.     pause(H1, -1);
  97.     }
  98.  
  99.     /**
  100.      * Pause a while, and draw the low&high water marks.
  101.      * @see SortAlgorithm
  102.      */
  103.     void pause(int H1, int H2) {
  104.     h1 = H1;
  105.     h2 = H2;
  106.     if (kicker != null) {
  107.         repaint();
  108.     }
  109.     try {Thread.sleep(20);} catch (InterruptedException e){}
  110.     }
  111.  
  112.     /**
  113.      * Initialize the applet.
  114.      */
  115.     public void init() {
  116.     String at = getParameter("alg");
  117.     if (at == null) {
  118.         at = "BubbleSort";
  119.     }
  120.  
  121.     algName = at + "Algorithm";
  122.     scramble();
  123.  
  124.     resize(100, 100);
  125.     }
  126.  
  127.     /**
  128.      * Paint the array of numbers as a list
  129.      * of horizontal lines of varying lenghts.
  130.      */
  131.     public void paint(Graphics g) {
  132.     int a[] = arr;
  133.     int y = size().height - 1;
  134.  
  135.     // Erase old lines
  136.     g.setColor(Color.lightGray);
  137.     for (int i = a.length; --i >= 0; y -= 2) {
  138.         g.drawLine(arr[i], y, size().width, y);
  139.     }
  140.  
  141.     // Draw new lines
  142.     g.setColor(Color.black);
  143.     y = size().height - 1;
  144.     for (int i = a.length; --i >= 0; y -= 2) {
  145.         g.drawLine(0, y, arr[i], y);
  146.     }
  147.  
  148.     if (h1 >= 0) {
  149.         g.setColor(Color.red);
  150.         y = h1 * 2 + 1;
  151.         g.drawLine(0, y, size().width, y);
  152.     }
  153.     if (h2 >= 0) {
  154.         g.setColor(Color.blue);
  155.         y = h2 * 2 + 1;
  156.         g.drawLine(0, y, size().width, y);
  157.     }
  158.     }
  159.  
  160.     /**
  161.      * Update without erasing the background.
  162.      */
  163.     public void update(Graphics g) {
  164.     paint(g);
  165.     }
  166.  
  167.     /**
  168.      * Run the sorting algorithm. This method is
  169.      * called by class Thread once the sorting algorithm
  170.      * is started.
  171.      * @see java.lang.Thread#run
  172.      * @see SortItem#mouseUp
  173.      */
  174.     public void run() {
  175.     try {
  176.         if (algorithm == null) {
  177.         algorithm = (SortAlgorithm)Class.forName(algName).newInstance();
  178.         algorithm.setParent(this);
  179.         }
  180.         algorithm.init();
  181.         algorithm.sort(arr);
  182.     } catch(Exception e) {
  183.     }
  184.     }
  185.  
  186.     /**
  187.      * Stop the applet. Kill any sorting algorithm that
  188.      * is still sorting.
  189.      */
  190.     public synchronized void stop() {
  191.     if (kicker != null) {
  192.             try {
  193.         kicker.stop();
  194.             } catch (IllegalThreadStateException e) {
  195.                 // ignore this exception
  196.             }
  197.         kicker = null;
  198.     }
  199.     if (algorithm != null){
  200.             try {
  201.         algorithm.stop();
  202.             } catch (IllegalThreadStateException e) {
  203.                 // ignore this exception
  204.             }
  205.     }
  206.     }
  207.  
  208.  
  209.     /**
  210.      * For a Thread to actually do the sorting. This routine makes
  211.      * sure we do not simultaneously start several sorts if the user
  212.      * repeatedly clicks on the sort item.  It needs to be
  213.      * synchronoized with the stop() method because they both
  214.      * manipulate the common kicker variable.
  215.      */
  216.     private synchronized void startSort() {
  217.     if (kicker == null || !kicker.isAlive()) {
  218.         scramble();
  219.         repaint();
  220.         kicker = new Thread(this);
  221.         kicker.start();
  222.     }
  223.     }
  224.  
  225.  
  226.     /**
  227.      * The user clicked in the applet. Start the clock!
  228.      */
  229.     public boolean mouseUp(java.awt.Event evt, int x, int y) {
  230.     startSort();
  231.     return true;
  232.     }
  233. }
  234.